﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using pcsse.App_Code;
using pcsse.App_Code.entity;

namespace pcsse.management
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Priority"] != null)
                {
                    int pri = Convert.ToInt32(Session["Priority"].ToString());
                    if (pri <= 2)
                    {
                        Response.Redirect("~/default.aspx");
                    }
                }
                partyDv.Visible = false;
            }
            errLb.Text = "";
        }

        protected void newBt_Click(object sender, EventArgs e)
        {
            partyDv.Visible = false;
            pinfoDv.Visible = true;
            pinfoDv.ChangeMode(DetailsViewMode.Insert);
        }

        protected void PartyBt_Click(object sender, EventArgs e)
        {
            pinfoDv.Visible = false;
            partyDv.Visible = true;
            partyDv.ChangeMode(DetailsViewMode.ReadOnly);
        }

        protected void pinfoBt_Click(object sender, EventArgs e)
        {
            partyDv.Visible = false;
            pinfoDv.Visible = true;
            pinfoDv.ChangeMode(DetailsViewMode.ReadOnly);
        }

        protected void pinfoDv_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
                errHandle(e.Exception.InnerException);
            }else{
                pGv.SelectedIndex = -1;
            }
        }

        protected void pinfoDv_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInEditMode = true;
                errHandle(e.Exception.InnerException);
            }
        }

        protected void errHandle(Exception e)
        {
            if (e is ArgumentException)
            {
                string argu = ((ArgumentException)e).ParamName;
                errLb.Text = "参数[";
                errLb.Text += argu;
                errLb.Text += "]不合法!";
            }
            else if (e is NoNullAllowedException)
            {
                errLb.Text = "一个或多个字段为空!";
            }
            else if (e is System.Data.Common.DbException)
            {
                errLb.Text = "数据库异常，请稍后再试";
            }
            else
            {
                errLb.Text = "未知错误，请稍后再试";
            }
        }

        protected void partyDv_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInEditMode = true;
                errHandle(e.Exception.InnerException);
            }
        }

        protected void filterBt_Click(object sender, EventArgs e)
        {
            string filterstring = "";
            string numberTb = ((TextBox)(filterDv.FindControl("numberTb"))).Text;
            string nameTb = ((TextBox)(filterDv.FindControl("nameTb"))).Text;
            string bfilter = "";
            string sfilter = "";
            DropDownList bfilterDdl = ((DropDownList)(filterDv.FindControl("bfilterDdl")));
            DropDownList sfilterDdl = ((DropDownList)(filterDv.FindControl("sfilterDdl")));
            if (bfilterDdl.SelectedIndex != 0)
            {
                bfilter = bfilterDdl.SelectedValue;
            } 
            if (sfilterDdl.SelectedIndex != 0)
            {
                sfilter = sfilterDdl.SelectedValue;
            }
            filterstring += string.Format("[{0}] LIKE '%{1}%'", pGv.Columns[1].SortExpression, numberTb);
            filterstring += string.Format(" AND [{0}] LIKE '%{1}%'", pGv.Columns[2].SortExpression, nameTb);
            filterstring += string.Format(" AND [{0}] LIKE '%{1}%'", pGv.Columns[3].SortExpression, bfilter);
            filterstring += string.Format(" AND [{0}] LIKE '%{1}%'", pGv.Columns[5].SortExpression, sfilter);
            pGvOds.FilterExpression = filterstring;
        }
    }
}